﻿Imports BUS
Imports DTO
Public Class DanhSachLop

    Private Sub DanhSachLop_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim _Bushs As New CHocSinhBus()
        Dim _danhsachhs As New List(Of CHocSinhDTO)
        _danhsachhs = _Bushs.LayDanhSachHS()
        Dim _Buslop As New ClopBUS()
        Dim _danhsachlop As New List(Of CLopDTO)
        _danhsachlop = _Buslop.LayDanhSachLop()
        cbblop.DataSource = _danhsachlop
        cbblop.DisplayMember = "TenLop"
        cbblop.ValueMember = "MaLop"
        Dim i As Integer
        For i = 0 To _danhsachhs.Count - 1
            dtgvdanhsachhs.Rows.Add(i + 1, _danhsachhs(i).MaHS, _danhsachhs(i).HoTen, _danhsachhs(i).GioiTinh, _danhsachhs(i).NgaySinh, _danhsachhs(i).DiaChi)
        Next
    End Sub

    Private Sub dtgvdanhsachhs_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgvdanhsachhs.SelectionChanged

        If dtgvdanhsachhs.CurrentRow.Index >= 0 Then
            txthoten.Text = dtgvdanhsachhs.CurrentRow.Cells("clhoten").Value
            txtngaysinh.Text = dtgvdanhsachhs.CurrentRow.Cells("clnamsinh").Value
            txtgioitinh.Text = dtgvdanhsachhs.CurrentRow.Cells("clgioitinh").Value
            txtdiachi.Text = dtgvdanhsachhs.CurrentRow.Cells("cldiachi").Value
        End If

    End Sub

    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Try

            If dtgvdanhsachhs.Rows.Count >= 2 Then
                Dim _CTLdto As New CChiTietLopDTO()
                Dim _Lopdto As New CLopDTO()
                Dim _Index As Integer = cbblop.SelectedIndex
                _Lopdto = cbblop.Items(_Index)
                _CTLdto.MaLop = _Lopdto.MaLop
                _CTLdto.MaHocSinh = dtgvdanhsachhs.CurrentRow.Cells("clmahs").Value
                Dim _Bus As New CChiTietLopBUS()
                Dim _Bussiso As New ClopBUS()
                Dim _buskt As New CChiTietLopBUS()
                Dim _busdmh As New CDiemMonHocBUS()
                If _buskt.KiemTraSiSo(_Lopdto) > 0 Then
                    dtgvdanhsachhs.Rows.RemoveAt(dtgvdanhsachhs.CurrentRow.Index)
                    If _Bus.ThemChiTietLop(_CTLdto) > 0 Then
                        _Bussiso.CapNhatSiSo(_Lopdto)
                        CapNhatSiSo()
                        _busdmh.ChuyensangDiemMonHoc(_CTLdto)
                        MessageBox.Show("Chon Lop Thanh Cong")

                    Else
                        MessageBox.Show("Chon Lop That Bai")
                    End If
                Else
                    MessageBox.Show("lop " + _Lopdto.TenLop + " da du Hoc Sinh")
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("Loi --> " + ex.ToString())
        End Try

    End Sub

    Public Sub CapNhatSiSo()
        Dim _lopdto As New CLopDTO()
        _lopdto = cbblop.SelectedItem
        txtsiso.Text = _lopdto.SiSo.ToString()
    End Sub

    Private Sub cbblop_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbblop.SelectedIndexChanged
        CapNhatSiSo()
    End Sub
End Class